package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.mongo.UserLocation;
import com.tanhua.domain.vo.UserLocationVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜附近-服务接口实现类
 */
@Service
@Slf4j
public class UserLocationApiImpl implements UserLocationApi{

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 上报地理位置
     * //纬度 113.929778
     * //经度 22.582111
     */
    @Override
    public void addLocation(Double latitude, Double longitude, Long userId, String addrStr) {
        //1.先根据userId查询地理位置是否上报过
        Query query1 = new Query(Criteria.where("userId").is(userId));
        UserLocation ul = mongoTemplate.findOne(query1, UserLocation.class);
        long nowTime = System.currentTimeMillis();//当前时间
        if(ul == null) {
            //2.如果没有上报过，保存地理位置
            UserLocation userLocation = new UserLocation();
            userLocation.setId(ObjectId.get());//主键id
            userLocation.setUserId(userId);//当前上报用户id
            //地图服务 纬度、经度对应坐标轴x,y
            userLocation.setLocation(new GeoJsonPoint(latitude,longitude));
            userLocation.setAddress(addrStr);//地址位置描述
            userLocation.setCreated(nowTime);//上报地理位置时间
            userLocation.setUpdated(nowTime);//更新地理位置时间
            userLocation.setLastUpdated(nowTime);//最后一次上报地理位置时间
            mongoTemplate.save(userLocation);
        }
        else {
            //3.如果上报过，则更新地理位置
            Query query2 = new Query(Criteria.where("id").is(ul.getId()));
            Update update = new Update();
            update.set("address",addrStr);//地址位置描述
            update.set("updated",nowTime);//上报地理位置时间
            update.set("lastUpdated",ul.getUpdated());//上报地理位置时间
            update.set("location",new GeoJsonPoint(latitude,longitude));
            mongoTemplate.updateFirst(query2, update, UserLocation.class);
        }
    }

    /**
     * 搜附近
     */
    @Override
    public List<UserLocationVo> searchNearUser(Long userId, String distance) {
        //1.根据当前用户id查询当前用户位置
        Query ulQuery = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(ulQuery, UserLocation.class);
        GeoJsonPoint location = userLocation.getLocation();
        //2.传入当前用户位置  以及  搜索距离  返回List<UserLocation>
        Circle circle = new Circle(location,
                new Distance(Double.valueOf(distance)/1000, Metrics.KILOMETERS));//以半径来搜索附近用户
        Query query = new Query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocationList = mongoTemplate.find(query, UserLocation.class);
        //3.将List<UserLocation> 转为 List<UserLocationVo>
        List<UserLocationVo> userLocationVoList = UserLocationVo.formatToList(userLocationList);
        //4.返回结果
        log.debug("******************"+userLocationVoList.toString());
        return userLocationVoList;
    }
}
